package com.wzy.xxgl.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.wzy.common.exception.ServiceException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.wzy.common.annotation.Log;
import com.wzy.common.core.controller.BaseController;
import com.wzy.common.core.domain.AjaxResult;
import com.wzy.common.enums.BusinessType;
import com.wzy.xxgl.domain.XxglXsxx;
import com.wzy.xxgl.service.IXxglXsxxService;
import com.wzy.common.utils.poi.ExcelUtil;
import com.wzy.common.core.page.TableDataInfo;

/**
 * 信息管理-学生信息Controller
 * 
 * @author wzy
 * @date 2025-10-12
 */
@RestController
@RequestMapping("/xxgl/xsxx")
public class XxglXsxxController extends BaseController
{
    @Autowired
    private IXxglXsxxService xxglXsxxService;

    /**
     * 查询信息管理-学生信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(XxglXsxx xxglXsxx)
    {
        startPage();
        List<XxglXsxx> list = xxglXsxxService.selectXxglXsxxList(xxglXsxx);
        return getDataTable(list);
    }

    /**
     * 导出信息管理-学生信息列表
     */
    @Log(title = "信息管理-学生信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, XxglXsxx xxglXsxx)
    {
        List<XxglXsxx> list = xxglXsxxService.selectXxglXsxxList(xxglXsxx);
        ExcelUtil<XxglXsxx> util = new ExcelUtil<XxglXsxx>(XxglXsxx.class);
        util.exportExcel(response, list, "信息管理-学生信息数据");
    }

    /**
     * 获取信息管理-学生信息详细信息
     */
    @GetMapping(value = "/{studentId}")
    public AjaxResult getInfo(@PathVariable("studentId") Long studentId)
    {
        return success(xxglXsxxService.selectXxglXsxxByStudentId(studentId));
    }

    /**
     * 获取信息管理-学生信息详细信息（根据当前登录用户ID）
     */
    @GetMapping(value = "/detail/{currentUserId}")
    public AjaxResult getInfoByCurrentUserId(@PathVariable("currentUserId") Long currentUserId)
    {
        XxglXsxx xxglXsxx = xxglXsxxService.getInfoByCurrentUserId(currentUserId);
        if(xxglXsxx == null){
            XxglXsxx xxglXsxxTemp = new XxglXsxx();
            xxglXsxxTemp.setIsBind(false);
            return success(xxglXsxxTemp);
        }
        return success(xxglXsxx);
    }

     /**
     * 查询学生信息表 学生下拉框内容
     */
    @GetMapping(value = "/selectData")
    public AjaxResult getStudentSelect()
    {
        return success(xxglXsxxService.getStudentSelect());
    }

    /**
     * 新增信息管理-学生信息
     */
    @Log(title = "信息管理-学生信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody XxglXsxx xxglXsxx)
    {
        XxglXsxx detail = xxglXsxxService.getInfoByCurrentUserId(xxglXsxx.getGlId());
        if (detail != null){
            return warn("该关联用户已被其他用户绑定，请重新绑定!");
        }
        return toAjax(xxglXsxxService.insertXxglXsxx(xxglXsxx));
    }

    /**
     * 修改信息管理-学生信息
     */
    @Log(title = "信息管理-学生信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody XxglXsxx xxglXsxx)
    {
        XxglXsxx detail = xxglXsxxService.getInfoByCurrentUserId(xxglXsxx.getGlId());
        if (detail != null && !detail.getStudentId().equals(xxglXsxx.getStudentId())){
            return warn("该关联用户已被其他用户绑定，请重新绑定!");
        }
        return toAjax(xxglXsxxService.updateXxglXsxx(xxglXsxx));
    }

    /**
     * 删除信息管理-学生信息
     */
    @Log(title = "信息管理-学生信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{studentIds}")
    public AjaxResult remove(@PathVariable Long[] studentIds)
    {
        return toAjax(xxglXsxxService.deleteXxglXsxxByStudentIds(studentIds));
    }
}
